package com.springapp.utils;

import com.springapp.sqlite.ConfigModel;

import java.io.File;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;

/**
 * Created by tandan on 16-10-11.
 */
public class GlobalUtils {

    //public static List<ConfigModel>  ConfigModelList = null;
    //public static ConfigModel  ConfigModelGlobal = null;

    private static String githubDowloadPath = "";
    private static String basePath = "";
    private static String projectName = "";
    private static String inspectCatalog;
    private static String fullPath;

    static {
        githubDowloadPath ="http://localhost:7005/rancher-catalog.git";
        projectName = "rancher-catalog";
        String configProperties = System.getProperty("user.dir");
        basePath = configProperties  + File.separator + ".." + File.separator + "webapps" + File.separator + projectName  ;

    }


    public void init() {

        String configProperties = System.getProperty("user.dir");
        System.out.println("config file path  : " + configProperties);
        githubDowloadPath = "";
        basePath = "";
        projectName = "";
        inspectCatalog = "";//扫描目录

        //PropertiesLoader propertiesLoader = new PropertiesLoader(configProperties + File.separator + "config.properties");
        Map<String, String> map  = HandleProperties.readProperties(configProperties + File.separator + "config.properties");
        githubDowloadPath = map.get("githubDowloadPath");
        basePath = map.get("basePath") ;
        projectName = map.get("projectName");
        inspectCatalog = map.get("inspectCatalog");

        fullPath = basePath + File.separator + projectName + File.separator + inspectCatalog ;

        System.out.println("fullPath: " +fullPath );
        //init从配置文件初始化

    }

    public static String getGithubDowloadPath() {
        return githubDowloadPath;
    }

    public static void setGithubDowloadPath(String githubDowloadPath) {
        GlobalUtils.githubDowloadPath = githubDowloadPath;
    }

    public static String getBasePath() {
        return basePath;
    }

    public static void setBasePath(String basePath) {
        GlobalUtils.basePath = basePath;
    }

    public static String getProjectName() {
        return projectName;
    }

    public static void setProjectName(String projectName) {
        GlobalUtils.projectName = projectName;
    }

    public static String getInspectCatalog() {
        return inspectCatalog;
    }

    public static void setInspectCatalog(String inspectCatalog) {
        GlobalUtils.inspectCatalog = inspectCatalog;
    }

    public static String getFullPath() {
        return fullPath;
    }

    public static void setFullPath(String fullPath) {
        GlobalUtils.fullPath = fullPath;
    }

    public static void copy(Object o1, Object o2) throws Exception {
        Field[] field = o1.getClass().getDeclaredFields();
        Method[] mm = o1.getClass().getDeclaredMethods();
        for(int j=0 ; j<field.length ; j++){
            String name = field[j].getName();
            String type = field[j].getGenericType().toString();
            if(type.equals("class java.lang.String")){
                Method m = o1.getClass().getMethod("get"+toUpperCaseFirstOne(name));
                String value = (String) m.invoke(o1);
                String value2 = (String) m.invoke(o2);
                if(value == null && value2!=null){
                    m = o1.getClass().getMethod("set"+toUpperCaseFirstOne(name), String.class);
                    m.invoke(o1, value2);
                }
            } else if("int".equals(type)) {
                Method m = o1.getClass().getMethod("get"+toUpperCaseFirstOne(name));
                Integer value = (Integer) m.invoke(o1);
                Integer value2 = (Integer) m.invoke(o2);
                if(value == null){
                    m = o1.getClass().getMethod("set"+toUpperCaseFirstOne(name), Integer.class);
                    m.invoke(o1, value2);
                }
            }
        }
    }



    /**
     * 首字母转大写
     */
     public static String toUpperCaseFirstOne(String s) {
        if(Character.isUpperCase(s.charAt(0)))
            return s;
        else
            return (new StringBuilder()).append(Character.toUpperCase(s.charAt(0))).append(s.substring(1)).toString();
     }



}
